/**
 * Copyright (C), 2015-2020, XXX有限公司
 * FileName: UserContoller
 * Author:   sky
 * Date:     2020/4/20 13:41
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package cn.szh.controller;

import cn.szh.domain.Role;
import cn.szh.domain.UserInfo;
import cn.szh.service.RoleService;
import cn.szh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.naming.Name;
import java.lang.annotation.Retention;
import java.util.List;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author sky
 * @create 2020/4/20
 * @since 1.0.0
 */
@Controller
@RequestMapping("/user")
public class UserContoller {
    @Autowired
    //@Qualifier("userService")
    private UserService userService;
    @Autowired
    private RoleService roleService;
    /**
     * 展示所有用户基本信息
     * @return
     */
    @RequestMapping("/findAll.do")
    @Secured("ROLE_ADMIN")
    public ModelAndView findAll(){
        ModelAndView mv=new ModelAndView();
        List<UserInfo> users = userService.findAll();
        mv.addObject("userList",users);
        mv.setViewName("user-list");
        return mv;
    }

    @RequestMapping("/findById.do")
    public ModelAndView findById(@RequestParam(name = "id")Integer id){
        ModelAndView mv=new ModelAndView();
        UserInfo userInfo = userService.findByid(id);
        mv.addObject("user",userInfo);
        mv.setViewName("user-show");
        return mv;
    }

    @RequestMapping("/save.do")
    public String save(UserInfo userInfo){
        userService.save(userInfo);

        return "redirect:/user/findAll.do";
    }

    @RequestMapping("/findUserByIdAndAllRole.do")
    public ModelAndView findUserByIdAndAllRole(@RequestParam(name = "id")Integer id){
        ModelAndView mv=new ModelAndView();
        UserInfo userInfo = userService.findByid(id);
        List<Role> otherRoles = roleService.findOtherRoleById(id);
        mv.addObject("user",userInfo);
        mv.addObject("roleList",otherRoles);
        mv.setViewName("user-role-add");
        return mv;
    }
    @RequestMapping("/addRoleToUser.do")
    @Secured("ROLE_ADMIN")
    public String addRoleToUser(@RequestParam(name ="id")Integer uid,@RequestParam(name="ids")Integer[] rids){
        for (int rid : rids) {
            roleService.addRoleToUser(uid,rid);
        }

        return "forward:findById.do";
    }

}
